from pydantic import BaseModel, Field, validator
from typing import Optional

class WorkbenchBase(BaseModel):
    benchName: str
    status: Optional[str] = Field("active", description="工台状态")
    cycleTime: Optional[int] = Field(None, description="工台节拍")

    @validator("status")
    def status_valid(cls, v):
        if v is not None and v not in ("active", "inactive","maintenance"):
            raise ValueError("status只能为active或inactive或maintenance")
        return v

    @validator("cycleTime")
    def cycle_time_valid(cls, v):
        if v is not None and (not isinstance(v, int) or v <= 0):
            raise ValueError("cycleTime必须为正整数")
        return v

class WorkbenchCreate(WorkbenchBase):
    benchId: str

class WorkbenchUpdate(BaseModel):
    benchName: Optional[str] = None
    status: Optional[str] = None
    cycleTime: Optional[int] = None

    @validator("status")
    def status_valid(cls, v):
        if v is not None and v not in ("active", "inactive","maintenance"):
            raise ValueError("status只能为active或inactive或maintenance")
        return v

    @validator("cycleTime")
    def cycle_time_valid(cls, v):
        if v is not None and (not isinstance(v, int) or v <= 0):
            raise ValueError("cycleTime必须为正整数")
        return v

class WorkbenchDetail(WorkbenchBase):
    benchId: str
    createdBy: Optional[str] = Field(None, description="创建人(后端自动生成)")
    createdAt: Optional[str] = Field(None, description="创建时间(后端自动生成)")
    updatedBy: Optional[str] = Field(None, description="更新人(后端自动生成)")
    updatedAt: Optional[str] = Field(None, description="更新时间(后端自动生成)")
    benchTypeName: Optional[str] = Field(None, description="工台类型名称") 